package com.dhecp.core.utils.dingtalk;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @ClassName HttpUtils 钉钉HTTP工具类
 * @Description TODO
 * @Author alula
 * @Date 2022-03-04 16:38
 */
public class DtHttpUtils {
    private static final Logger logger = LoggerFactory.getLogger(DtHttpUtils.class);

    public static JSONObject sendGet(String url) {
        JSONObject jsonObject = null;
        jsonObject = httpRequest(url, "GET", null);
        return jsonObject;
    }

    public static JSONObject sendPost(String url) {
        JSONObject jsonObject = null;
        jsonObject = httpRequest(url, "POST", null);
        return jsonObject;
    }

    public static JSONObject sendPost(String url, String output) {
        JSONObject jsonObject = null;
        jsonObject = httpRequest(url, "POST", output);
        return jsonObject;
    }

    private static JSONObject httpRequest(String request, String requestMethod, String outputStr) {
        JSONObject jsonObject = null;
        StringBuffer buffer = new StringBuffer();
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        try {
            logger.debug("【钉钉HTTP】", "http请求request:{},method:{},output{}", new Object[]{request, requestMethod, outputStr});

            URL url = new URL(request);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setConnectTimeout(3000);
            connection.setReadTimeout(30000);
            connection.setUseCaches(false);
            connection.setRequestMethod(requestMethod);
            connection.setRequestProperty("Accept", "application/json;charset=UTF-8");
            connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            if (outputStr != null) {
                OutputStream out = connection.getOutputStream();
                out.write(outputStr.getBytes("UTF-8"));
                out.close();
            }
            inputStream = connection.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
            reader = new BufferedReader(inputStreamReader);
            String line;
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
            reader.close();
            inputStreamReader.close();
            inputStream.close();
            inputStream = null;
            connection.disconnect();
            return JSONObject.parseObject(buffer.toString());
        } catch (Exception e) {
            logger.error("【钉钉HTTP】", "http请求error:{}", new Object[]{e.getMessage()});
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
                if (inputStreamReader != null) {
                    inputStreamReader.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                logger.error("【钉钉HTTP】", "http请求error:{}", new Object[]{ex.getMessage()});
            }
        }
        return  jsonObject;
    }
}
